package com.wjd.wshow.common.resource;

import com.wjd.wshow.common.resource.loader.ResourceLoader;
import org.apache.commons.io.FileUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

public abstract class AbstractResource implements Resource {

    @Override
    public long getSize() {
        long size = 0;
        try {
            Path path = ResourceLoader.getLoader().getAbsPath(this);
            size = Files.size(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return size;
    }

    @Override
    public InputStream getInputStream() throws IOException {
        InputStream is;
        Path path = ResourceLoader.getLoader().getAbsPath(this);
        if (path == null) {
            throw new IOException("Path is null!");
        }
        is = FileUtils.openInputStream(path.toFile());
        return is;
    }

    /**
     * 缩略图是否存在
     * @return true/false
     */
    public boolean exist() {
        boolean exist;
        Path path = ResourceLoader.getLoader().getAbsPath(this);
        exist = Files.exists(path);
        return exist;
    }

}
